package org.bee.controller;

import jakarta.annotation.Resource;
import org.bee.ResultTool;
import org.bee.enums.ResultCode;
import org.bee.exception.ExceptionUtils;
import org.bee.model.IdList;
import org.bee.model.Permission;
import org.bee.model.result.Result;
import org.bee.secuirty.annotation.ApiAuth;
import org.bee.service.RolePermissionService;
import org.bee.service.ext.PermissionExtService;
import org.bee.transaction.TransUtils;
import org.bee.transaction.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色接口权限管理
 */
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {
    private static final Logger logger = LoggerFactory.getLogger(RolePermissionController.class);

    @Resource
    private PermissionExtService permissionExtService;
    @Resource
    private RolePermissionService rolePermissionService;
    /**
     * 获取角色接口权限
     * @return 角色接口权限
     */
    @GetMapping("getRolePermission")
    @ApiAuth(value = "rolePermission.getRolePermission",description = "角色接口权限")
    public Result getRoleButtonRole(String roleId){
        Result result = ResultTool.success();
        List<Permission> permissionByUserId = permissionExtService.getPermissionByRoleId(roleId);
        List<String> permissionList = new ArrayList<>();
        for (Permission permission : permissionByUserId) {
            permissionList.add(permission.getPermissionId());
        }
        result.setData(permissionList);
        return result;
    }

    /**
     * 保存角色接口权限
     * @param idList 角色id和接口权限id列表
     * @return 保存结果
     */
    @PostMapping("saveRolePermission")
    @ApiAuth(value = "rolePermission.saveRolePermission",description = "保存角色接口权限")
    public Result saveRolePermission(@RequestBody IdList<String> idList) {
        Result result = ResultTool.success();
        if(idList == null || idList.getId() == null || idList.getList() == null){
            result = ResultTool.fail(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        Transaction trans = null;
        try {
            trans= TransUtils.getTrans();
            trans.begin();
            rolePermissionService.deleteByRoleId(idList.getId());
            List<String> list = idList.getList();
            for (String permissionId : list) {
                rolePermissionService.saveRolePermission(idList.getId(),permissionId);
            }
            trans.commit();
        } catch (Exception e) {
            TransUtils.rollback(trans);
            result = ResultTool.fail();
            logger.error(e.getMessage(), ExceptionUtils.getFullStackTrace(e));
            return result;
        }
        return result;
    }
}
